$(document).ready(function(){
    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FTPMSKMAG  : 'FTPMSKMAG',
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        $setMaskListDiv:$("#setMaskListDiv"),
        $setMaskListGrd:$("#setMaskListGrd"),
        setMaskListPg	:"setMaskListPg",
        $toolMaskListDivDiv:$("#toolMaskListDivDiv"),
        $toolMaskListGrd:$("#toolMaskListGrd"),
        toolMaskListPg	:"toolMaskListPg",
        $toolSelNo		:$("#toolSelNo"),
        buttons:{
            $query_btn    :$("#query_btn"),
            $mount_btn    :$("#mount_btn"),
            $unmount_btn    :$("#unmount_btn")
        },
        spans:{
            $toolIdSpan       	:      $("#toolIdSpan")        ,
            $toolStatSpan       :      $("#toolStatSpan")       ,
            $toolStatDscSpan    :      $("#toolStatDscSpan")      ,
            $toolModeSpan   	:      $("#toolModeSpan")     ,
            $recipeIdSpan       :      $("#recipeIdSpan")
        }
    };
    var controlsFunc={
        iniMaskGridInfo:function(){
            var itemInfoCM = [
                {
                    name  : 'mask_id_fk',
                    index : 'mask_id_fk',
                    label : MASK_NO_TAG,
                    width : 200
                },{
                    name : 'mask_stat',
                    index :'mask_stat',
                    label : MASK_STAT_TAG,
                    width : 200
                }];
            domObj.$toolMaskListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 370,
                autowidth : true,//宽度根据父元素自适应
                shrinkToFit : false,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
//					multiselect : true,
                pager : domObj.toolMaskListPg,
                gridComplete : function() {
                },
                onSelectRow : function(id) {
                }
            });
        },
        setMaskGridInfo:function(){
            var itemInfoCM = [
                {
                    name  : 'mask_id_fk',
                    index : 'mask_id_fk',
                    label : MASK_NO_TAG,
                    width : 200
                },{
                    name : 'mask_stat',
                    index :'mask_stat',
                    label : MASK_STAT_TAG,
                    width : 200
                }];
            domObj.$setMaskListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 370,
                autowidth : true,//宽度根据父元素自适应
                shrinkToFit : false,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
//				multiselect : true,
                pager : domObj.setMaskListPg,
                gridComplete : function() {
                },
                onSelectRow : function(id) {
                }

            });
        },
        iniToolSel: function(){
            domObj.$toolSelNo.empty();
            var inObj, outObj;
            var iary ={
                unit_typ : "MAIN"
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom .addSelectArr(domObj.$toolSelNo, outObj.oary, "tool_id","tool_id","",true);
            }
        },
        addToolMaskGrid:function(dataObj){
            var newRowID = getGridNewRowID("#toolMaskListGrd");
            $("#toolMaskListGrd").jqGrid("addRowData",newRowID,dataObj);
        },
        queryFunc:function(){
            $("#toolMaskListGrd").jqGrid("clearGridData");
            var iary = {
                data_cate : 'MKTL'
            };
            var inTrxObj = {
                trx_id      : 'FBPBISDAT' ,
                action_flg  : 'Q'        ,
                iary        : [iary]
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                var tbl_cnt = comParseInt( outTrxObj.tbl_cnt);
                for (var i = 0; i < tbl_cnt; i++) {

                    var mask_id_fk = outTrxObj.oary[i].data_ext;
                    var iary = {
                        mask_id_fk :  mask_id_fk
                    };
                    var inObj = {
                        trx_id      : 'FTPMSKMAG' ,
                        action_flg  : 'Q'        ,
                        iary        : [iary]
                    };
                    var  outObj = comTrxSubSendPostJson(inObj);
                    if(  outObj.rtn_code == VAL.NORMAL ) {
                        var data ={
                            mask_id_fk : mask_id_fk,
                            mask_stat : 'unmount'
                        };
                        if (outObj.tbl_cnt == 0) {
                            controlsFunc.addToolMaskGrid(data);
                        }else {
                            var mask_stat = outObj.oary[0].mask_stat;
                            if (mask_stat === 'U') {
                                controlsFunc.addToolMaskGrid(data);
                            }
                        }
                    }
                }
            }
        },
        selChangeFunc:function(){
            var tool_id = domObj.$toolSelNo.val();
            var inObj,outObj ,inTrxObj,outTrxObj;
            var iary ={ tool_id:tool_id};
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'I',
                iary		:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                domObj.spans.$toolIdSpan.html(tool_id);
                domObj.spans.$toolStatSpan.html(outObj.oary[0].tool_stat);
                domObj.spans.$toolStatDscSpan.html(outObj.oary[0].tool_dsc);
                domObj.spans.$toolModeSpan.html(outObj.oary[0].tool_run_mode);
      		    domObj.spans.$recipeIdSpan.html(outObj.oary[0].tool_stat);
            }
            inTrxObj = {
                trx_id      : VAL.T_FTPMSKMAG,
                action_flg  : 'Q',
                iary		:[{
                    tool_id_fk  : tool_id,
                    mask_stat   : 'M'
                }]
            };
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if (outTrxObj.rtn_code == VAL.NORMAL) {
                $("#setMaskListGrd").jqGrid("clearGridData");

                for (var i = 0; i < outTrxObj.tbl_cnt; i++) {

                    var mask_id_fk= outTrxObj.oary[i].mask_id_fk;
                    var data = {mask_id_fk : mask_id_fk,
                        mask_stat : 'mount'};
                    var newRowID = getGridNewRowID("#setMaskListGrd");
                    $("#setMaskListGrd").jqGrid("addRowData",newRowID,data);
                }

            }
        },
        unmountFunc: function(){
            var inObj,outObj,iary;
            var rowId=domObj.$setMaskListGrd.jqGrid('getGridParam','selrow');
            if (!rowId) {
                showErrorDialog("","请选择治工具信息！");
                return;
            }
            var rowData = domObj.$setMaskListGrd.jqGrid('getRowData',rowId);
            var mask_id_fk = rowData.mask_id_fk;
            iary = {
                mask_id_fk:mask_id_fk,
                mask_stat :'U'
            }
            inObj={
                trx_id      :VAL.T_FTPMSKMAG,
                action_flg  :'A',
             /*   evt_usr		:'C1602020',*/
                iary        :[iary]
            }
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                controlsFunc.selChangeFunc();
                controlsFunc.queryFunc();
            }
        },
        mountFunc:function(){
            var rowId=domObj.$toolMaskListGrd.jqGrid('getGridParam','selrow');
            var tool_id = domObj.$toolSelNo.val();
            if (!rowId) {
                showErrorDialog("","请选择治工具信息！");
                return;
            }
            if (!tool_id) {
                showErrorDialog("","请选择设备代码");
                return;
            }
            var rowData = domObj.$toolMaskListGrd.jqGrid('getRowData',rowId);
            var mask_id_fk = rowData.mask_id_fk;
            iary = {
                mask_id_fk:mask_id_fk,
                mask_stat :'M',
                tool_id_fk:tool_id
            }
            inObj={
                trx_id      :VAL.T_FTPMSKMAG,
                action_flg  :'B',
                evt_usr		:VAL.EVT_USER,
                iary        :[iary]
            }
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                controlsFunc.queryFunc();
                controlsFunc.selChangeFunc();
            }
        }
    };
    var iniButtonAction =function(){
        domObj.buttons.$query_btn.click(function(){
            controlsFunc.queryFunc();
        });
        domObj.$toolSelNo.change(function(){
            controlsFunc.selChangeFunc();
        });
        domObj.buttons.$mount_btn.click(function(){
            controlsFunc.mountFunc();
        });
        domObj.buttons.$unmount_btn.click(function(){
            controlsFunc.unmountFunc();
        });
    };

    function initFunc(){
        controlsFunc.iniMaskGridInfo();
        controlsFunc.setMaskGridInfo();
        controlsFunc.iniToolSel();

        iniButtonAction();
    }
    initFunc();
    //表格自适应
    // function resizeFnc(){
    //    var offsetBottom, divWidth;
    //
    //    grdDivWidth = $("#divTotal").width();
    //    divWidth = $("#toolMaskListDiv").width();
    //    offsetBottom =$(window).height() - $("#toolMaskListDiv").offset().top;
    //    $("#toolMaskListDiv").height(offsetBottom * 0.95);
    //    $("#toolMaskListGrd").setGridWidth(grdDivWidth * 0.45);
    //    $("#toolMaskListGrd").setGridHeight(offsetBottom * 0.99 - 51);
    //
    //    // domObj.$lrBtnDiv.offset({left:domObj.$window.width()/2-30});
    //    divWidth = $("#setMaskListDiv").width();
    //    offsetBottom = $(window).height() - $("#setMaskListDiv").offset().top;
    //    $("#setMaskListDiv").height(offsetBottom * 0.95);
    //    $("#setMaskListGrd").setGridWidth(grdDivWidth * 0.45);
    //    $("#setMaskListGrd").setGridHeight(offsetBottom * 0.99 - 51);
    // };
    // resizeFnc();
    // domObj.$window.resize(function() {
    //    resizeFnc();
    // });
    //模态框拖动
    $(".modal-content").draggable();

    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1;
        grdDivWidth = $("#divLeft").width();
        offsetBottom = $(window).height() - $("#toolMaskListDiv").offset().top;

        $("#toolMaskListDiv").width(grdDivWidth*0.98);
        $("#toolMaskListDiv").height(offsetBottom*0.99);
        $("#toolMaskListGrd").setGridWidth(grdDivWidth*0.97);
        $("#toolMaskListGrd").setGridHeight(offsetBottom*0.95-51);
        //2
        grdDivWidth1 = $("#fieLeft").width();
        offsetBottom1 = $(window).height() - $("#setMaskListDiv").offset().top;

        $("#setMaskListDiv").width(grdDivWidth1*0.98);
        $("#setMaskListDiv").height(offsetBottom1*0.99);
        $("#setMaskListGrd").setGridWidth(grdDivWidth1*0.97);
        $("#setMaskListGrd").setGridHeight(offsetBottom1*0.95-51);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});/**
 * Created by ADMIN on 2017/12/27.
 */
